/**
 * 
 */
package com.celephais.common.client.model;

import java.util.List;

import com.celephais.common.client.model.TableColumn.DisplayType;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Eugene Shen
 * This DTO represents an unowned reference to another object.
 * It will store both the key and a displayable name for the referenced
 * object.
 */
public class ReferenceUI implements TransferObject {

	public static final String KEY = "key".intern();
	public static final String NAME = "name".intern();
	
	public static final TableColumn[] cols = {
		new TableColumn(KEY, "Key", false, DisplayType.TEXT_BOX),
		new TableColumn(NAME, "Name", true, DisplayType.TEXT_BOX)
	};
	
	private String key;
	private String name;
	
	public static ReferenceUI createReference(TransferObject obj) {
		if (obj == null) {
			return null;
		}
		else {
			return new ReferenceUI(obj.getKey(), obj.getName());
		}
	}
	
	public ReferenceUI() {
		super();
	}
	
	public ReferenceUI(String key, String name) {
		this();
		setKey(key);
		setName(name);
	}
	
	@Override
	public TableColumn[] getColumns() {
		return cols;
	}

	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	
	@Override
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getValue(String columnName) {
		if (KEY.equalsIgnoreCase(columnName)) {
			return getKey();
		}
		else if (NAME.equalsIgnoreCase(columnName)) {
			return getName();
		}
		else {
			return null;
		}
	}

	@Override
	public void setValue(String columnName, String value) {
		if (KEY.equalsIgnoreCase(columnName)) {
			setKey(value);
		}
		else if (NAME.equalsIgnoreCase(columnName)) {
			setName(value);
		}
	}

	@Override
	public String toString() {
		return getName();
	}

	// REFERENCE-RELATED.
	
	@Override
	public ReferenceUI getReference(String columnName) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Reference type, but getReference is not implemented?");
	}
	@Override
	public void listValidReferences(String columnName, AsyncCallback<List<? extends TransferObject>> callback) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Reference type, but listValidReferences is not implemented?");
	}
	@Override
	public void setReference(String columnName, ReferenceUI ref) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Reference type, but setReference is not implemented?");
	}
	
	// CHILDREN-RELATED.
	
	@Override
	public List<TransferObject> getChildren(String columnName) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Child type, but getChildren is not implemented?");		
	}
	@Override
	public TransferObject createChild(String columnName) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Child type, but createChild is not implemented?");
	}
	@Override
	public boolean addChild(String columnName, int position, TransferObject newChild) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Child type, but addChild is not implemented?");
	}
	@Override
	public boolean removeChild(String columnName, int position) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Child type, but removeChild is not implemented?");
	}
	@Override
	public boolean moveChild(String columnName, int oldPosition, int newPosition) throws UnimplementedException {
		throw new UnimplementedException(columnName + " is listed as a Child type, but moveChild is not implemented?");
	}
}
